package hadoop.topN.question2;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class StuMapper2 extends Mapper<LongWritable, Text, StuBean, NullWritable> {
    StuBean sb = new StuBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //切分
        String[] line = value.toString().split(",");

        //求个人平均分
        double sum = 0;
        double count = 0;
        for (int i = 2; i < line.length; i++) {
            sum += Integer.parseInt(line[i]);
            count++;
        }
        double selfAvg = sum / count;
        String courseName = line[0];
        String stuName = line[1];

        //封装对象
        sb.setStuName(stuName);
        sb.setCourseName(courseName);
        sb.setSelfAvg(selfAvg);

        //写入上下文
        context.write(sb, NullWritable.get());
    }
}
